﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace TelasSinacorWPF
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // Get all instances of Notepad running on the local
                // computer.
                Process[] localByName = Process.GetProcessesByName("sinacorapp");

                listBox1.Items.Clear();
                foreach (ProcessModule module in localByName[0].Modules)
                {
                    if (module.FileName.ToLower().IndexOf("smartclient") >= 0 && RetornaValidacao(module.FileName.ToLower()))
                        listBox1.Items.Add(module.FileName);
                }
            }
            catch
            {
                MessageBox.Show("Não foi possível encontrar o Sinacor");
            }
        }

        private bool RetornaValidacao(string p)
        {
            if (p.IndexOf("menu") >= 0)
                return false;
            if (p.IndexOf("languages") >= 0)
                return false;
            if (p.IndexOf("commom") >= 0)
                return false;
            if (p.IndexOf("themes") >= 0)
                return false;

            return true;
        }

        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                string selectedvalue = listBox1.SelectedItem.ToString();
                lblModulo.Content = selectedvalue.Split('.')[2].ToString();
                lblProjeto.Content = selectedvalue.Split('.')[3].ToString();
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            string modulo = lblModulo.Content.ToString();
            string projeto = lblProjeto.Content.ToString();

            ResolverPath(modulo, projeto);
        }

        private void ResolverPath(string modulo, string projeto)
        {
            string pasta = string.Empty;

            if (modulo == "BMF")
                pasta = "SinaOMF";
            else if (modulo == "Bovespa")
                pasta = "SinaORD";
            else if (modulo == "Clientes" || modulo == "Cliente")
                pasta = "SinaCLI";
            else if (modulo == "Tesouraria")
                pasta = "SinaTES";
            else if (modulo == "ContaCorrente")
                pasta = "SinaCCO";


            string path = "C:\\DSEPROJ\\" + pasta + "\\DEV\\CDSB\\Source\\UI\\" + modulo + "\\" + projeto;

            if (System.IO.Directory.Exists(path))
                AbrirDiretorio(path);

        }

        private void AbrirDiretorio(string path)
        {
            System.Diagnostics.ProcessStartInfo procStartInfo =
           new System.Diagnostics.ProcessStartInfo(@"explorer.exe", "\"" + path + "\"");
            procStartInfo.RedirectStandardOutput = true;
            procStartInfo.UseShellExecute = false;
            procStartInfo.CreateNoWindow = false;
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo = procStartInfo;
            proc.Start();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {

            //Process[] localByName = Process.GetProcessesByName("sinacor");

            //string selectedvalue = listBox1.SelectedItem.ToString();

            //System.Reflection.Assembly assembly = System.Reflection.Assembly.Load(selectedvalue);




            //System.Threading.Thread threadAbreTela = new System.Threading.Thread(delegate()
            //{
            //    assembly.EntryPoint.Invoke("TotalporClienteView", null);
            //    assembly.EntryPoint.Invoke("Show", null);
            //});

            //localByName[0].Threads.Add(threadAbreTela);

            //threadAbreTela.SetApartmentState(System.Threading.ApartmentState.STA);
            //threadAbreTela.Start();

            
        }
    }
}
